<template>
  <div>
    <div class="manage">
      <div class="tableContainer">
        <div class="searchByNum" style="float: left;margin-bottom: 10px;">
          <span style="margin-right: 10px;font-size: 16px;">由数量区间显示库存：</span>
          <el-input
            v-model="begin"
            @keyup.enter.exact.native="searchByNum($event)"
          />
          <el-input
            v-model="end"
            @keyup.enter.exact.native="searchByNum($event)"
          />
          <el-link :underline="false" style="font-size: 18px;" icon="el-icon-search" @click="searchByNum($event),clickHandler($event)" />
        </div>
        <div style="float: right;margin-bottom: 10px;">
          <el-link :underline="false" @click="reload()"><i class="el-icon-refresh" style="font-size: 24px;" title="刷新数据" /></el-link>
        </div>
        <el-table
          ref="table"
          v-loading="loading"
          border
          :data="tableData[currentPage - 1]"
          style="width: 100%"
        >
          <el-table-column
            align="center"
            prop="id"
            label="图书编号"
          />
          <el-table-column
            align="center"
            prop="bookName"
            label="书名"
            width="180"
          />
          <el-table-column
            align="center"
            prop="number"
            label="库存/本"
            width="100"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.number <= 10 ? 'danger' : 'success'"
                disable-transitions
              >{{ scope.row.number }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
          >
            <template slot="header">
              <el-input
                v-model="input"
                size="medium"
                placeholder="输入图书id或书名,按下回车搜索"
                @keyup.enter.exact.native="search($event)"
              />
            </template>
            <template slot-scope="scope">
              <el-button
                type="success"
                icon="el-icon-plus"
                circle
                size="medium"
                title="增加库存"
                @click="addInventories(scope.row),clickHandler($event)"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-area">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="12"
          :total="allBookNum"
          @current-change="page"
        />
      </div>
    </div>

  </div>
</template>

<script>
import userApi from '@/api/user'
export default {
  inject: ['reload'],
  data() {
    return {
      tableData: [],
      allBookNum: 0,
      currentPage: 1,
      loading: true,
      input: '',
      begin: '',
      end: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      userApi.getInventories().then(resp => {
        this.loading = true
        const data = resp.data.data.inventories
        this.allBookNum = data.length
        for (let i = 0; i < data.length; i += 12) {
          this.tableData.push(data.slice(i, i + 12))
        }
        this.loading = false
      })
    },
    page(currentPage) {
      this.currentPage = currentPage
    },
    async search(event) {
      event && event.target.blur()
      let getByName = []
      let getById = []
      let data = []
      await userApi.getInventoriesLikeName(this.input).then(resp => {
        getByName = resp.data.data.inventories
      })
      await userApi.getInventoriesByIds(this.input).then(resp => {
        getById = resp.data.data.inventories
      })
      getByName.length === 0 ? data = getById : data = getByName
      if (data) {
        this.tableData = []
        this.allBookNum = data.length
        for (let i = 0; i < data.length; i += 12) {
          this.tableData.push(data.slice(i, i + 12))
        }
        this.$message.success('搜索已完成！')
      }
    },
    addInventories(data) {
      this.$prompt('请输入需要增加的库存量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$/,
        inputErrorMessage: '请输入大于0的整数'
      }).then(({ value }) => {
        const form = []
        form.push({ bookId: data.id, number: value })
        userApi.addInventories(form).then(resp => {
          resp.data.code === 200 ? this.$message.success('添加库存成功！') : this.$message.success('添加库存失败！')
        })
        this.initData()
        this.reload()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    searchByNum(event) {
      event && event.target.blur()
      userApi.getNotEnoughInventories(this.begin, this.end).then(resp => {
        if (resp.data.code === 200) {
          this.tableData = []
          const data = resp.data.data.inventories
          this.allBookNum = data.length
          for (let i = 0; i < data.length; i += 12) {
            this.tableData.push(data.slice(i, i + 12))
          }
          this.$message.success('数据已更新！')
        } else {
          this.$message.error('请输入正确的查询区间！')
        }
      }, err => {
        this.$message.error('查询错误！')
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>
.manage {
  display: flex;
  width: 850px;
  padding: 30px;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  box-shadow: rgba(0, 0, 0, 0.15) 0px 15px 25px, rgba(0, 0, 0, 0.05) 0px 5px 10px;
}
.tableContainer {
  width: 100%;
}
.pagination-area {
  margin-top: 30px;
  margin-bottom: 30px;
}
.searchByNum .el-input {
  width: 40px;
  margin-right: 20px;
}
.searchByNum >>> .el-input__inner {
  width: 100%;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #DCDFE6;
  padding-left: 2px;
  padding-right: 0;
}
.searchByNum >>> .el-input__inner:focus {
  border-color: #409EFF;
  outline: 0;
}
</style>
